<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Registration
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class RegisterController extends Zend_Controller_Action
{
	private $registry; // registry
	private $settings; // board-settings
	private $translate; // translation
	
	public function init()
	{
		// registry
		$this->registry 		= Zend_Registry::getInstance();
		
		// settings
		$this->settings			= $this->registry->get('settings');
		
		// lang
		$this->translate		= $this->registry->get('lang');
		
		// flash messenger
		$this->view->flashMessenger	= $this->_helper->FlashMessenger;
		
		// assign lang to view
		$this->view->translate	= $this->translate;
	}
	
	/*
	 * displays the register form
	 *
	 * @param null
	 * 
	 * @return Zend_View
	 */
	public function indexAction()
	{
		// db
		$user		= new User();
		
		// form
		$register 	= new RegisterForm();
		
		// post data
		if ($this->_request->isPost() && $register->isValid($_POST)) 
		{
			$values		= $register->getValues();
			$act_code	= strtolower(substr(md5(microtime()),0,25));
			
			$check		= $user->fetchAll('mail="' . $register->getValue('mail') . '"');
			
			if($check->count() == 0)
			{
				$newUserArray	= array(
					'group_uid'			=> '1',
					'activation_code'	=> $act_code,
					'role'				=> 'user',
					'language'			=> $values['language'],
					'username'			=> $values['username'],
					'mail'				=> $values['mail'],
					'password'			=> md5($values['password']),
					'create_date'		=> new Zend_Db_Expr('NOW()')
				);
				
				if($user->insert($newUserArray))
				{
					$subject	= $this->translate->_('register_mail_subject');
					$text		= MHDEV_Sprintf(
						$this->translate->_('register_mail_text'),
						array(
							'%url'	=> $this->settings['domain_name'],
							'%code' => $act_code,
							'%user'	=> $values['username']
						)
					);

					// Prepare and send registered mail with code
					$mail = new Zend_Mail();

					$mail->setBodyHtml($text);
					$mail->setFrom($this->translate->_('mail_system'), $this->translate->_('mail_system_name'));
					$mail->addTo($values['mail'], $values['username']);
					$mail->setSubject($subject);
					$mail->send();
					
					// finish
					$this->_redirect('/register/done');
				}
			}
			else
			{
				$mailform = $register->getElement('mail');
				$mailform->addError($translate->_('error_register_mail2'));
			}
		}
		else
		{
			$register->populate($_POST);
		}
		
		// view
		$this->view->form = $register;
	}
	
	
	
	/*
	 * only view, shows the "done"-action
	 */
	public function doneAction()
	{
		
	}
	
	
	
	/*
	 * activate a user by a unique key
	 * 
	 * @param null
	 * 
	 * @return Zend_View
	 */
	public function authAction()
	{
		// param
		$uid 	= $this->_request->getParam('uid', 0);

		// secure redirect
		if(strlen($uid) == 0) { $this->_redirect('/'); }
		
		// db
		$user	= new User();
		
		// query
		$check	= $user->fetchAll('activation_code="' . $uid . '"');
		
		// check
		if($check->count() == 1)
		{
			$userRow = $check->toArray();
			
			$userUpdateArray = array(
				'is_active'			=> 1,
				'activation_code'	=> '',
			);
			$user->update($userUpdateArray,'uid=' . $userRow[0]['uid']);
			
			return $this->view->error = 0;
		}
		else
		{
			return $this->view->error = 1;
		}
	}



	/*
	 * Generates a new password
	 * 
	 * @param null
	 * 
	 * @return Zend_View
	 */
	public function newpassAction()
	{
		// param
		$postData	= $this->_request->getPost();
		$error		= false;
		
		// db
		$user		= new User();
		
		// form
		$form		= new NewPassForm();
		
		// handle post
		if($this->_request->isPost() && $form->isValid($postData))
		{
			$getUser	= $user->fetchAll('uid > 0 AND mail="' . $form->getValue('mail') . '"');
			
			if($getUser->count() > 0)
			{
				$resUser	= $getUser->toArray();
				
				/*
				 * generate a password
				 *
				 * @param int $size password-size
				 * 
				 * @return int $result gen. password
				 */
				function password($size) 
				{
					$result = ""; 
					srand((double)microtime()*1000000); 
					for($i=0; $i < $size; $i++) 
					{
						$num = rand(48,120); 
						while (($num >= 58 && $num <= 64) || ($num >= 91 && $num <= 96)) 
						$num = rand(48,120); 
						           
						$result .= chr($num); 
					} 
					return $result; 
				}
				
				$new_password	= password(8);
				
				$user->update(array(
					'password'	=> md5($new_password)
				), 'mail="' . $form->getValue('mail') . '"');
				
				// Prepare and send mail with new password
				$mail = new Zend_Mail();
				
				$subject	= $this->translate->_('register_newpass_subject');
				$text		= MHDEV_Sprintf(
					$this->translate->_('register_newpass_text'),
					array(
						'%url'	=> $this->settings['domain_name'],
						'%pw'	=> $new_password,
						'%user'	=> $resUser[0]['username']
					)
				);
				
				$mail->setBodyHtml($text);
				$mail->setFrom($this->translate->_('mail_system'), $this->translate->_('mail_system_name'));
				$mail->addTo($resUser[0]['mail'], $resUser[0]['username']);
				$mail->setSubject($subject);
				if($mail->send())
				{
					$error = 'success';
				}
			
			}
			else
			{
				$error = 1;
			}
		}
		else
		{
			$form->populate($postData);
		}
		
		// view
		$this->view->form	= $form;
		$this->view->error	= $error;
	}
}
?>